snapshot: Reorder color matrix nodes containing a transform node
authorTimm Bäder <mail@baedert.org>
Fri, 29 Nov 2019 08:14:55 +0000 (09:14 +0100)
committerTimm Bäder <mail@baedert.org>
Tue, 3 Dec 2019 07:40:34 +0000 (08:40 +0100)
A color matrix node that contains a transform node can also be expressed
the other way around, as a transform node containing a color matrix
node.

In the general case, the color matrix node will have to draw its
child to a texture so it can color every pixel of that texture, but the
renderers can short-cut this if the child of the color matrix node is
already a texture node. So if we have a node tree like

Color Matrix
    - Transform
        - Texture

The renderer would have to either check the grandchild of the color
matrix or simply fall back to rendering the transform node to a texture.

In the new configuration:

Transform
    - Color Matrix
        - Texture

The renderer can easily see that the child node of the color matrix node
is a texture, and skip rendering it to a texture.

This is for example happening in current Adwaita for spinners, which are
rotated symbolics.

gtk/gtksnapshot.c

index 661abc33c4b652352fedcdb5fc9bf1d2e9300dad..a7f55e6bbb30f6b896ae92327da2c9113de4145e 100644 (file)
@@ -433,7 +433,6 @@ merge_color_matrix_nodes (const graphene_matrix_t *matrix2,
                           const graphene_vec4_t   *offset2,
                           GskRenderNode           *child)
 {
-  GskRenderNode *grandchild = gsk_render_node_ref (gsk_color_matrix_node_get_child (child));
   const graphene_matrix_t *mat1 = gsk_color_matrix_node_peek_color_matrix (child);
   const graphene_vec4_t *offset1 = gsk_color_matrix_node_peek_color_offset (child);
   graphene_matrix_t mat2 = *matrix2;
@@ -454,11 +453,9 @@ merge_color_matrix_nodes (const graphene_matrix_t *matrix2,
   graphene_vec4_add (&off2, offset1, &off2);
 
   graphene_matrix_multiply (mat1, &mat2, &mat2);
-  gsk_render_node_unref (child);
-  child = NULL;
 
-  result = gsk_color_matrix_node_new (grandchild, &mat2, &off2);
-  gsk_render_node_unref (grandchild);
+  result = gsk_color_matrix_node_new (gsk_color_matrix_node_get_child (child),
+                                      &mat2, &off2);
 
   return result;
 }
@@ -480,6 +477,30 @@ gtk_snapshot_collect_color_matrix (GtkSnapshot      *snapshot,
       result = merge_color_matrix_nodes (&state->data.color_matrix.matrix,
                                          &state->data.color_matrix.offset,
                                          node);
+      gsk_render_node_unref (node);
+    }
+  else if (gsk_render_node_get_node_type (node) == GSK_TRANSFORM_NODE)
+    {
+      GskRenderNode *transform_child = gsk_transform_node_get_child (node);
+      GskRenderNode *color_matrix;
+
+      if (gsk_render_node_get_node_type (transform_child) == GSK_COLOR_MATRIX_NODE)
+        {
+          color_matrix = merge_color_matrix_nodes (&state->data.color_matrix.matrix,
+                                                   &state->data.color_matrix.offset,
+                                                   transform_child);
+        }
+      else
+        {
+          color_matrix = gsk_color_matrix_node_new (transform_child,
+                                                    &state->data.color_matrix.matrix,
+                                                    &state->data.color_matrix.offset);
+        }
+      result = gsk_transform_node_new (color_matrix,
+                                       gsk_transform_node_get_transform (node));
+      gsk_render_node_unref (color_matrix);
+      gsk_render_node_unref (node);
+      node = NULL;
     }
   else
     {